#!/bin/bash

#
# afdsmgrd-aaf-integration.sh -- by Dario Berzano <dario.berzano@cern.ch>
#

export BATCH=0
if [ "$1" == "-b" ] || [ "$1" == "--batch" ]; then
  BATCH=1
fi

# aaf.cf
source $ALICE_PROOF_AAF_DIR/aaf.cf

# ROOT
#export ROOTSYS="/pool/PROOF-AAF/alien_packages/VO_ALICE/ROOT/v5-28-00e-1/v5-28-00e-1"
export ROOTSYS="$ALICE_PROOF_AAF_DIR/root"

# Discovers ROOT version
export ROOT_VER
if [ -L "$ROOTSYS" ]; then
  ROOT_VER=$(readlink $ROOTSYS)
fi
ROOT_VER=$(basename $ROOT_VER | cut -d'-' -f1,2,3)

# Shortcut...
export XPD="$ALICE_PROOF_AAF_PROOF_XPROOFD_DIR"

# Configuration file
export CFG="$ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/afdsmgrd.conf"
#export CFG="$ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/prf-main.cf"

# Sysconfig
export SYSCFG="$ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/afdsmgrd.sysconfig"

# Datasets
export DS="$ALICE_PROOF_AAF_PROOF_DIR/dataset"

# Provide user with relevant variables
echo ""
echo "=== Integration of afdsmgrd in AAF ==="
echo ""
echo "Using afdsmgrd from ROOT:"
echo "  $ROOTSYS"
echo ""
echo "ROOT version is:"
echo "  $ROOT_VER"
echo ""
echo "The PROOF master is:"
echo "  $ALICE_PROOF_AAF_PROOF_MASTER"
echo ""
echo "Datasets are stored in:"
echo "  $DS"
echo ""
echo "AliEn:"
echo "  $ALICE_PROOF_AAF_ALIEN_DIR"
echo ""
echo "ApMon server to contact:"
echo "  $ALICE_PROOF_AAF_XROOTD_MONALISA"
echo ""
echo "Prefix for MonALISA notifications (same as xrootd SE):"
echo "  $ALICE_PROOF_AAF_XROOTD_SE_NAME"
echo ""

if [ $BATCH == 0 ]; then
  echo -n ">> Press Enter to continue, Ctrl+C to abort"
  read R
fi

# Configuration file for afdsmgrd
cat > "$CFG" <<EOF
#
# See $ROOTSYS/etc/proof/afdsmgrd.conf.example for help
#

set PMASTER=$ALICE_PROOF_AAF_XROOTD_REDIRECTOR
xpd.datasetsrc file url:$DS mss:root://\$PMASTER:1094 opt:Cq:Av:Ar: rw=1
dsmgrd.urlregex alien://(.*)$ root://\$PMASTER/$AAF_AFDSMGR_XRD_PREFIX\$1
dsmgrd.sleepsecs 30
dsmgrd.scandseveryloops 10
dsmgrd.parallelxfrs $AAF_AFDSMGR_MAX_TRANSFERS
dsmgrd.corruptafterfails $AAF_AFDSMGR_MAX_CORRUPT_AFTER_FAILS
dsmgrd.stagecmd $ROOTSYS/etc/proof/afdsmgrd-xrd-stage-verify.sh "\$URLTOSTAGE" "\$TREENAME"
dsmgrd.cmdtimeoutsecs 1800
dsmgrd.notifyplugin $ROOTSYS/etc/proof/lib/libafdsmgrd_notify_apmon.so
dsmgrd.apmonurl apmon://$ALICE_PROOF_AAF_XROOTD_MONALISA
dsmgrd.apmonprefix $ALICE_PROOF_AAF_XROOTD_SE_NAME

verifier.urlregex alien://(.*)$ root://\$PMASTER/\$AAF_AFDSMGR_XRD_PREFIX\$1
verifier.sleepsecs 6
verifier.scandseveryloops 10
verifier.parallelverifies 800
verifier.verifycmd $ROOTSYS/etc/proof/afverifier-xrd-locate.sh "\$REDIRURL"
verifier.erasecmd $ROOTSYS/etc/proof/afverifier-xrd-rm.sh "\$REDIRURL"
verifier.maxfailures 3

EOF

# Startup configuration file for afdsmgrd
cat > "$SYSCFG" <<EOF
#
# See $ROOTSYS/etc/proof/sysconfig/afdsmgrd.example for help
#

export AFDSMGRD_PROG=$ROOTSYS/bin/afdsmgrd
export AFDSMGRD_CONF=$CFG
export ROOTSYS=$ROOTSYS
export AFDSMGRD_LIBS=$ALICE_PROOF_AAF_ALIEN_DIR/api/lib
export AFDSMGRD_EXTCMD_LIBS=$ALICE_PROOF_AAF_ALIEN_DIR/api/lib
export AFDSMGRD_EXTCMD_PATH=$ALICE_PROOF_AAF_ALIEN_DIR/api/bin
export AFDSMGRD_LIBEXEC=$ROOTSYS/etc/proof
export AFDSMGRD_LOGDIR=$ALICE_PROOF_AAF_DIR/logs/afdsmgrd
export AFDSMGRD_LOGFILE=\$AFDSMGRD_LOGDIR/afdsmgrd.log
export AFDSMGRD_PIDFILE=$ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/afdsmgrd.pid
export AFDSMGRD_USER=$ALICE_PROOF_AAF_USER
export AFDSMGRD_GROUP=$ALICE_PROOF_AAF_GROUP
export AFDSMGRD_LOGLEVEL=normal
EOF

# Symbolic links
#ln -nfs $SYSCFG /etc/sysconfig/afdsmgrd
#ln -nfs $ROOTSYS/etc/proof/init.d/afdsmgrd /etc/init.d/afdsmgrd

### TEMPORARY FIX ###
export ESC_ROOTSYS=$(echo $ROOTSYS | sed --expression='s/\//\\\//g')
find $ROOTSYS/etc/proof -name 'af*.sh' | xargs -l \
  sed --in-place --expression='s/\/opt\/aliroot\/data\/src\/trunk\/apps\/root\/root\/work\/root-'$ROOT_VER'/'"$ESC_ROOTSYS"'/g'
### END OF TEMPORARY FIX ###

# Final results
echo ""
echo "=== Integration finished ==="
echo ""
echo "Configuration file (might be integrated with prf-main.cf):"
echo "  $CFG"
echo ""
echo "Startup configuration file:"
echo "  $SYSCFG"
echo ""
#echo "Symbolic links in /etc:"
#echo "  $(ls -l /etc/init.d/afdsmgrd)"
#echo "  $(ls -l /etc/sysconfig/afdsmgrd)"
#echo ""
echo "Have a good day!"
echo ""
